import React from "react"
import { Route, Redirect, Switch } from "react-router-dom"

const checkLoginStatus = () => localStorage.getItem("logining") ? true : false

const RenderRoutes = function ({ routes }) {
    return (
        <Switch>
            {routes.map((route, i) => (
                <Route
                    key={route.key || i}
                    path={route.path}
                    exact={route.exact}
                    strict={route.strict}
                    render={props => {
                        const isLoggedIn = checkLoginStatus()
                        const pathname = props.location.pathname
                        if (isLoggedIn || pathname === "/login" || pathname === "/register") {
                            return route.render ?
                                route.render({ ...props, route: route }) :
                                route.component && <route.component {...props} route={route} />
                        } else {
                            return <Redirect to="/login" />
                        }
                    }}
                />
            ))}
        </Switch>
    )
}
export default RenderRoutes;